package com.atguigu.yygh.cmn.controller;

import com.atguigu.yygh.cmn.service.DictService;
import com.atguigu.yygh.common.result.R;
import com.atguigu.yygh.model.cmn.Dict;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author:ahang
 * @create:2022-08-30 9:41
 * @Description:
 */
@Api(description = "数据字典接口")
@RestController
@RequestMapping("/admin/cmn/dict")
//@CrossOrigin
public class DictController {

    @Autowired
    private DictService dictService;

    /**
     * 根据数据id查询数据列表
     */
    @ApiOperation(value = "根据数据id查询子数据列表")
    @GetMapping("findChildData/{id}")
    public R findChildData(@PathVariable Long id){
        List<Dict> list = dictService.findChildData(id);
        return R.ok().data("list",list);
    }

    /**
     * 数据导出
     */
    @GetMapping("exportData")
    public void exportData(HttpServletResponse response){
        dictService.exportData(response);
    }

    /**
     * 数据导入
     */
    @PostMapping("importData")
    public R importData(MultipartFile file){
        dictService.inportData(file);
        return R.ok();
    }

    /**
     * 根据parentdiccode和value来查找对应的名称
     */
    @GetMapping("getName/{dictCode}/{value}")
    public String getName(@PathVariable("dictCode") String dictcode,@PathVariable("value") String value){
        String name = dictService.getNameByParentDictCodeAndValue(dictcode, value);
        return name;
    }
    @GetMapping("getName/{value}")
    public String getName(@PathVariable("value") String value){
        String name = dictService.getNameByParentDictCodeAndValue("",value);
        return name;
    }

    /**
     * 根据dictcode查询所有的省份
     */
    @GetMapping("findByDictCode/{dictCode}")
    public R findByDictCode(@PathVariable("dictCode") String dictCode){
        List<Dict> list = dictService.findByDictcode(dictCode);
        return R.ok().data("list",list);
    }
}
